/**
 * 
 */
package com.mobi.Survey.lib.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.mobi.Survey.lib.R;
import com.mobi.Survey.lib.model.Credentials;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * LoginScreen.java This screen will prompt user to enter user name and password
 * 
 */
public class LoginScreen extends AbstractScreen {

	protected TextView txtErrorLogin;

	private EditText editPassword;

	protected EditText editUserId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		String userId = getUserIdFromPreferences(this);
		String psw = getPasswordFromPreferences();
		if (null != editUserId && null != userId && userId.length() > 0) {

			editUserId.setText(userId);
		}

		if (null != editPassword && null != psw && psw.length() > 0) {
			editPassword.setText(psw);
		}
	}
	


	private Credentials doPreDownloadSurveyWork() {
		txtErrorLogin.setVisibility(View.INVISIBLE);
		String psw = null;
		String userId = editUserId.getText().toString().trim();
		if (null != editPassword) {
			psw = editPassword.getText().toString();
		}

		return  new Credentials(userId, psw) ;
	}

	public void onLogin(){

		Credentials credentials = doPreDownloadSurveyWork();
		
 
 
		SurveyDownloadTask task = new SurveyDownloadTask(this,credentials);

		task.setCallback(new SurveyDownloadTask.SurveyDownloadCallback() {

			@Override
			public void onPostExecute(int result) {

				switch (result) {
				case SurveyDownloadTask.NO_SURVEY_FOUND:
					txtErrorLogin.setText("No data found for this user id.");
					txtErrorLogin.setVisibility(View.VISIBLE);
					
					break;
				case SurveyDownloadTask.LOGIN_ERROR:

					txtErrorLogin.setText(getString(R.string.error_invalid_user_id_or_password));
					txtErrorLogin.setVisibility(View.VISIBLE);
					break;

				case SurveyDownloadTask.CONNECT_ERROR:

					txtErrorLogin.setText(getString(R.string.server_connect_error));
					txtErrorLogin.setVisibility(View.VISIBLE);
					break;

				case SurveyDownloadTask.SWITCH_SCREEN:
					setUserPrefrences();
					switchToSurveyScreen();
					break;
				case SurveyDownloadTask.NO_INTERNET_CONNECTION:
					txtErrorLogin.setText(Messages.NO_INTERNET_CONNECTION);
					txtErrorLogin.setVisibility(View.VISIBLE);
					break;

				}

			}

			@Override
			public void inBackground() {
				populateUserBitmaps(true);
				//populateContextBitmaps();

			}
		});

		
		if (credentials.getUserId().length() == 0) {

			task.cancel(true);
		}
	 
		task.execute();
	
	}
	public void onClick(View view) {
		int id = view.getId();
		if (id == R.id.btnLogin) {
			onLogin();
		} else if (id == R.id.btnSkip) {
			onSkipClick();

		}
	}

	protected void onSkipClick() {
		getUserPrefrences();
		populateUserBitmaps(false);
		//populateContextBitmaps();

		switchToSurveyScreen();

	}

	@Override
	protected void initUiVars() {
		txtErrorLogin = (TextView) findViewById(R.id.txtErrorLogin);
		editPassword = (EditText) findViewById(R.id.editPassword);
		editUserId = (EditText) findViewById(R.id.editUserId);

	}

	@Override
	protected int getLayoutId() {

		return R.layout.login;
	}

	@Override
	protected void onRelease() {

	}
	

	public void onBackPressed() {
		String hardCodedUserId = getString(R.string.hardcoded_user_id);

		if (null == hardCodedUserId || hardCodedUserId.trim().length() == 0) {
			Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_HOME);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);

		} else {
			System.exit(0);
		}

	}

	protected void switchToSurveyScreen() {
		switchScreenTo(SurveyListScreen.class);
	}

}